$(function() {
	hideWaitMask();
	$('.install').click(function() {
		var me = $(this), pid = me.attr('href').replace('#', '');
		showWaitMask('正在安装插件...');
		$.ajax({
			url : './?Ctlr=InstallPlugin',
			data : {
				pid : pid
			},
			success : function(data) {
				if (data.success) {
					if (data.option) {
						openTab('./dashboard/?Ctlr=Options&group='+data.option,'option-'+data.option,'插件设置','icon_system');
					}
					window.location.reload();
				} else {
					alert(data.msg);
					hideWaitMask();
				}
			}
		});
		return false;
	});
	
	$('.uninstall').click(function() {
		if(!confirm('你确定要删除该插件?')){
			return false;
		}
		var me = $(this), pid = me.attr('href').replace('#', '');
		showWaitMask('正在卸载插件...');
		$.ajax({
			url : './?Ctlr=UnInstallPlugin',
			data : {
				pid : pid
			},
			success : function(data) {
				if (data.success) {					
					window.location.reload();
				} else {
					alert(data.msg);
					hideWaitMask();
				}
			}
		});
		return false;
	});
	$('.setting').click(function(){
		var me = $(this), option = me.attr('href').replace('#', ''),pid=me.attr('data-content');
		openTab('./dashboard/?Ctlr=Options&group='+option+'&pid='+pid,'option-'+option,'插件设置','icon_system');
		return false;
	});
	$('.btn-disabled').click(function(){
		var me = $(this), disabled = me.attr('data-content'),tip = disabled == '1'?'禁用':'启用',pid = me.attr('href').replace('#', '');
		if(!confirm('你确定要'+tip+'该插件?')){
			return false;
		}
		showWaitMask('正在'+tip+'插件...');
		$.ajax({
			url : './?Ctlr=ActivePlugin',
			data : {
				pid : pid,
				disabled:disabled
			},
			success : function(data) {
				if (data.success) {					
					window.location.reload();
				} else {
					alert(data.msg);
					hideWaitMask();
				}
			}
		});
		return false;
	});
	$('#upload-from').submit(function(){
		var me = $(this);
        showWaitMask('正在处理...');
        me.ajaxSubmit({
            transit:function(transit,url){               
                transitDialog(transit,url,uploadedCallback);
                hideWaitMask();
            },
            success :uploadedCallback
        });
        return false;
	});	
	$("#upload-file-dialog").dialog({
        autoOpen : false,        
        width : 750,
        height: 425,
        modal : true,
        buttons : {
            "解压" : function() {
                var uploader = $('#uploader').pluploadQueue();  
                if (uploader.files.length > 0 && uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
                    $(this).dialog("close");
                    $('#upload-from').submit();
                }else if(uploader.files.length > 0){
                    alert('队列中尚有文件未上传,请先上传或删除.');
                }else{
                    $(this).dialog("close");
                }
            },
            "取消" : function() {
                $(this).dialog("close");
            }
        }
    });
    
    $('#btn-upload').click(function() {
        $("#upload-file-dialog").dialog('open');
        return false;
    });
    // Spanish
    plupload.addI18n({
        'Select files' : '文件上传',
        'Add files to the upload queue and click the start button.' : '请点击“添加文件”按钮选择文件或将文件拖到队列.然后点击“上传”按钮。',
        'Filename' : '文件名',
        'Status' : '进度',
        'Size' : '大小',
        'Add files' : '添加文件',
        'Stop current upload' : '停止上传',
        'Start uploading queue' : '上传',
        'Uploaded %d/%d files': '已经上传 %d/%d 个文件',
        'N/A' : 'N/A',
        'Drag files here.' : '直接将文件拖动到这里',
        'File extension error.': '文件扩展名错误.',
        'File size error.': '文件尺寸错误.',
        'Init error.': '初始化失败.',
        'HTTP Error.': 'HTTP错误.',
        'Security error.': '安全错误.',
        'Generic error.': '一般错误.',
        'IO error.': 'IO错误.',
        'Stop Upload': '停止上传.',
        'Add Files': '添加文件',
        'Start upload': '上传',
        '%d files queued': '%d 个文件已排队'
    });    
    $("#uploader").pluploadQueue({      
        runtimes : 'flash,html4',       
        url : '../share/?Ctlr=PlUpload',
        max_file_size : '50mb',
        max_file_count: 100, // user can add no more then 20 files at a time
        chunk_size : '1mb',
        unique_names : true,
        multiple_queues : true,
        rename: true,        
        sortable: true,        
        filters : [            
            {title : "扩展插件", extensions : "zip"}            
        ],        
        flash_swf_url : '../../shared/plupload/plupload.flash.swf'
    });
    function uploadedCallback(data){
        if (data.success) {
            if(data.msg){
                alert(data.msg);
            }else{
            	alert('插件扩展上传完成.');
            }
            window.location.reload();
        }else{
        	 alert(data.msg);
        }
    }
});